package com.biao.java8.starem_test;

import java.time.Duration;
import java.time.Instant;
import java.util.OptionalLong;
import java.util.stream.LongStream;

public class Test09_parallel {
    public static void main(String[] args) {
        // Instant.now()  获取当前时间

        // 使用单线程
        Instant start = Instant.now();
        long sum = 0;
        for (int i = 0; i < 1000000000L; i++) {
            sum += i;
        }
        System.out.println(sum);
        Instant end = Instant.now();
        System.out.println("十亿求和耗费时间毫秒为：" + Duration.between(start, end).toMillis());

        // 多线程，使用的是parallel 并行流
        Instant start2 = Instant.now();
        LongStream longStream = LongStream.rangeClosed(0, 1000000000L);
        // parallel 并行流
        OptionalLong result = longStream.parallel().reduce((a, b) -> a + b);
        System.out.println(result.getAsLong());
        Instant end2 = Instant.now();
        System.out.println("十亿求和耗费时间毫秒为：" + Duration.between(start2, end2).toMillis());

    }
}
